//=====================================================================
// COPYSTR: Copyright (c) 2020 By HuachenLink, All Rights Reserved.
// FLENAME: atcmdtool.c
// CONTACT: liudongguo@huachen.link
// CREATED: 2020-09-12 21:52:00
// LTSVERN: 0.1.1
// LTSUPDT: 2020-12-03 09:41:33
//=====================================================================
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <mtd/mtd-user.h>
#include <linux/types.h>


//cat dev/mtd3 | hexdump | head -n 1 | cut -c 19-33
#define MAC_MTD_DEV "/dev/mtd3"
#define LAN_MAC_OFFSET  4
#define _mydebug_ 0

int mtd_fd = 0;
unsigned char lanmac[6] = {0}; 
unsigned char wanmac[6] = {0}; 
unsigned char macmtddev[10] = {0}; 

/*if device is r144,now r144 obtain mac addr from part factory*/
static int is_device_r144()
{
    return 1;
}

/*open device*/
static int open_mtd_device(char *mtd_dev)
{
    mtd_fd = open(mtd_dev, O_RDONLY);
    if(mtd_fd < 0)
        return -1;
    else
        return 0;
}

#if 0
void find_factory_mtd_part()
{
    /*no many part, just try 0-1-2-3-4...*/
    int index = 0, fd;
    struct mtd_info_user info;

    for(index = 0; index < 5; index ++)
    {
        fd = 0;
        memset(macmtddev, 0, sizeof(macmtddev));
        sprintf(macmtddev, "/dev/mtd%d", index);

        fd = open(macmtddev, O_RDONLY);
         // get MTD information
        if (ioctl(fd, MEMGETINFO, &info) < 0) {
            perror("ioctl - MEMGETINFO");
            close(fd);
            return -1;
        }

        if(!strcmp(info.flags), "Factory")
            break;
    }
}
#endif
/*read specify mtd device data*/
static int read_mtd_device(unsigned char *buffer, int offset, int size)
{    
    ssize_t bytes_read = 0;
    if(lseek(mtd_fd, offset, SEEK_SET) == (off_t)-1)
    {
        perror("lseek");
        free(buffer);
        close(mtd_fd);

        return -1;
    }
    /*ok, ready to read*/
    bytes_read = read(mtd_fd, buffer, size);
    if (bytes_read < 0) {
        perror("read");
        free(buffer);
        close(mtd_fd);

        return -1;
    }
    
    close(mtd_fd);
}

void handle_mac_address_lan(unsigned char *format_mac, unsigned char *mac)
{
    /*just like aabbccddeeff to aa:bb:cc:dd:ee:ff*/
    sprintf(format_mac, "%02x:%02x:%02x:%02x:%02x:%02x", 
            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

void handle_mac_address_wan(unsigned char *format_mac, unsigned char *mac)
{
    /*just like aabbccddeeff to aa:bb:cc:dd:ee:ff*/
    if(mac[5] == 0xff)
    {
        mac[4] += 0x1;
        mac[5] = 0;
    }
    else
        mac[5] += 0x1;
        
    sprintf(format_mac, "%02x:%02x:%02x:%02x:%02x:%02x", 
            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

int main(int argc, char **argv)
{
    unsigned char lan_format_mac[20] = {0}; 
    unsigned char wan_format_mac[20] = {0}; 
    unsigned char set_wan_mac_cmd[100] = {0};
    unsigned char set_lan_mac_cmd[100] = {0};
    
    open_mtd_device(MAC_MTD_DEV);
    read_mtd_device(lanmac, LAN_MAC_OFFSET, sizeof(lanmac));
    
    handle_mac_address_lan(lan_format_mac, lanmac);    
    handle_mac_address_wan(wan_format_mac, lanmac);

#if _mydebug_
    printf("mac is %s\n", lan_format_mac);
    printf("mac is %s\n", wan_format_mac);
#endif

    if(!strcmp(argv[1], "wan"))
        printf("%s\n", wan_format_mac);
    else if(!strcmp(argv[1], "lan"))
        printf("%s\n", lan_format_mac);
    else if(!strcmp(argv[1], "go"))
    {
        sprintf(set_lan_mac_cmd, "fw_setenv ethaddr %s", lan_format_mac);
        sprintf(set_wan_mac_cmd, "fw_setenv eth1addr %s", wan_format_mac);
        system(set_lan_mac_cmd);
        system(set_wan_mac_cmd);
    }

    return 0;
}